//
//  HPAlertProgressView.m
//  HPZZTong
//
//  Created by jrwang on 14-5-28.
//  Copyright (c) 2014年 Handpay. All rights reserved.
//

#import "HPAlertProgressView.h"


#define DEFAULT_BLUE [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0]

static const CGSize progressViewSize = { 200.0f, 30.0f };

@interface HPAlertProgressView ()

@property(nonatomic,strong) HPProgressView *curProgressView;

@property(nonatomic) CGFloat progress;

@property(nonatomic,copy) NSString *msgTitle;

@property(nonatomic, strong) UILabel *msglabel;

@property(nonatomic) float step;

@end

@implementation HPAlertProgressView

- (HPAlertProgressView *) initProgressViewWithTitle:(NSString *)title steps:(NSUInteger)steps
{
    self = [super initWithFrame:[[UIScreen mainScreen] bounds]];
    self.backgroundColor = [UIColor clearColor];
    
    _step = [[NSString stringWithFormat:@"%.2f",(float)(1.0/steps)] floatValue];
    
    HPProgressView *progressView = [[HPProgressView alloc] initWithFrame:CGRectMake(CGRectGetMidX(self.frame) - progressViewSize.width / 2.0f,
                                                                                    CGRectGetMidY(self.frame) - progressViewSize.height / 2.0f,
                                                                                    progressViewSize.width,
                                                                                    progressViewSize.height)];
    progressView.borderTintColor = [UIColor whiteColor];
    progressView.progressTintColor = [UIColor whiteColor];
    
    _msgTitle = title;
    _curProgressView = progressView;
    
    [self graphicsBackView:title];
    [self addSubview:_curProgressView];
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    return self;
}

- (void) updateProgress:(NSString *)tipInfo
{
    if (self.progress < 1.0f) {
        self.progress += self.step;
        
        //余数小于步长，特殊处理
        if ((1.0f - self.progress) < (self.step / 2)) {
            self.progress = 1.0f;
        }
        
        if (self.progress > 1.0f) {
            self.progress = 1.0f;
        }
        
        NSMutableString *mulStr = [NSMutableString stringWithString:[NSString stringWithFormat:@"%d",[[NSNumber numberWithFloat:(self.progress*100)] integerValue]]];
        [mulStr appendString:@"%"];
        
        if (tipInfo) {
            self.msglabel.text = tipInfo;
        } else {
            self.msglabel.text = [NSString stringWithFormat:@"当前完成进度：%@",mulStr];
        }
        
        [_curProgressView setProgress:self.progress animated:YES];
    }
}

- (void)graphicsBackView:(NSString *)msg
{
    UIView *messageBoxView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 220.0f, 160.0f)];
    messageBoxView.backgroundColor = [UIColor blackColor];
    messageBoxView.alpha = 0.8;
    messageBoxView.layer.cornerRadius = 10;
//    messageBoxView.layer.borderColor = [UIColor colorWithRed:215.0/255.0 green:215.0/255.0 blue:215.0/255.0 alpha:1.0].CGColor;
//    messageBoxView.layer.borderWidth = 1.0;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 10.0f, 180.0f, 60.0f)];
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.font = [UIFont systemFontOfSize:14];
    label.text = self.msgTitle;
    label.textAlignment = NSTextAlignmentLeft;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    self.msglabel = label;
    [messageBoxView addSubview:self.msglabel];
    [self addSubview:messageBoxView];
    messageBoxView.center = CGPointMake(self.center.x, self.center.y);
}

@end
